package NiuKe;

import java.util.HashMap;
import java.util.HashSet;

public class EvalRPN {
    public static void main(String[] args) {
        StringBuilder s;
        EvalRPN evalRPN = new EvalRPN();
        String[] tokens = new String[]{"2","1","3","+","-"};
        int i = evalRPN.evalRPN(tokens);
        System.out.println(i);
    }
    public HashMap<String,Count> countMap = new HashMap<>(4);
    public EvalRPN(){
        countMap.put("+",new Add());
        countMap.put("-",new Sub());
        countMap.put("*",new Mul());
        countMap.put("/",new Div());
    }
    public int evalRPN (String[] tokens) {
        // write code here
        String[] stack = new String[100];
        int top = -1;
        int result = 0;
        for(String t : tokens){
            if(countMap.containsKey(t)){
                result = countMap.get(t).count(Integer.parseInt(stack[top-1]), Integer.parseInt(stack[top]));
                top -= 1;
                stack[top] = String.valueOf(result);
                continue;
            }
            stack[++top] = t;
        }
        return result;
    }
}

interface Count{
    public abstract int count(int a,int b);
}
class Add implements Count{
    @Override
    public int count(int a, int b) {
        return a + b;
    }
}
class Sub implements Count{
    @Override
    public int count(int a, int b) {
        return a - b;
    }
}
class Mul implements Count{
    @Override
    public int count(int a, int b) {
        return a * b;
    }
}
class Div implements Count{
    @Override
    public int count(int a, int b) {
        return a / b;
    }
}